from pymysql import connect

from_connection = connect(
	host="生产环境IP",
	port=3306,
	user="dp",
	password="dp",
	db="dp"
)
from_cursor = from_connection.cursor()
select_sql = "SELECT sku_number, shop_id, user_name, is_vip, user_level, star_level, taste, environment, serve, average_price, favorite_dishes, thumbs_up, comment_time, comment FROM shop_comment LIMIT {},{};"

to_connection = connect(
	host="localhost",
	port=3306,
	user="root",
	password="",
	db="dp"
)
to_cursor = to_connection.cursor()
base_insert_sql = "INSERT INTO shop_comment(sku_number, shop_id, user_name, is_vip, user_level, star_level, taste, environment, serve, average_price, favorite_dishes, thumbs_up, comment_time, comment) VALUES "
insert_sql = "({!r}, {!r}, {!r}, {}, {}, {}, {!r}, {!r}, {!r}, {}, {!r}, {}, {!r}, {!r})"

offset, limit = 0, 1000
while True:
	number = from_cursor.execute(select_sql.format(offset, limit))
	data_list = list()
	for args in from_cursor.fetchall():
		data_list.append(insert_sql.format(*args))
	
	try:
		to_cursor.execute(base_insert_sql + ", ".join(data_list))
		
		to_connection.commit()
	except Exception as e:
		to_connection.rollback()
	
	if number < limit:
		break
	
	offset += limit
	print(offset)

to_cursor.close()
to_connection.close()
from_cursor.close()
from_connection.close()
